package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：shirongrong
 * @date ：Created in 2020/3/19
 * @description ：
 * @version: 1.0
 */
@Service
@Slf4j
public class CheckItemServiceImpl  implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;
    @Transactional
    @Override
    public void add(CheckItem checkItem) {
        log.debug(" CheckItemServiceImpl checkitem:{}",checkItem);
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.debug(" queryPageBean :{}",queryPageBean);
        //使用分页插件分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //动态彬姐limit，获取分页数据
        Page<CheckItem> pageData=checkItemDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(pageData.getTotal(),pageData.getResult());
    }

    @Transactional
    @Override
    public void deleteByid(Integer id) {
        // 检查检查组是否有检查项数据
        Long count = checkItemDao.countCheckItemById(id);
        // 如果有，抛出异常包含异常信息的异常
        if (count > 0){
            throw new RuntimeException("当前检查项有数据，不能删除");
        }
        checkItemDao.deleteCheckItemById(id);
    }

    @Override
    public CheckItem findById(Integer id) {

        return checkItemDao.findById(id);
    }
    @Transactional
    @Override
    public void edit(CheckItem checkItem) {
        log.debug(" checkItem:{}",checkItem);
        checkItemDao.edit(checkItem);

    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }


}
